home *** CD-ROM | disk | FTP | other *** search
- Path: news.clark.net!usenet
- From: gusty@clark.net (Harlan Messinger)
- Newsgroups: comp.lang.c++
- Subject: Re: Q:order of evaluation
- Date: Sun, 28 Jan 1996 19:24:05 GMT
- Organization: Clark Internet Services, Inc.
- Message-ID: <4egiid$j3f@clarknet.clark.net>
- References: <4dfhlu$a33$1@mhafn.production.compuserve.com> <hamilton-1801962045570001@dialup-147.austin.io.com> <4dpcfo$293@clarknet.clark.net> <hamilton-2401960104020001@dialup-86.austin.io.com> <3108c867.40236096@nntp.ix.netcom.com> <4eb6kq$ksf@gazette.tandem.com>
- NNTP-Posting-Host: gusty-ppp.clark.net
- Mime-Version: 1.0
- Content-Type: TEXT/PLAIN; charset=ISO-8859-1
- Content-Transfer-Encoding: 8bit
- X-Newsreader: Forte Free Agent 1.0.82
-
- Keep in mind, everyone, that the "hierarchy" of expression evaluation,
- including the precedence of expressions inside of parentheses, is a
- hierarchy of operators, not of operands.
-
- Given a + b * c, the subexpression b * c will be evaluated first and
- then the product will be added to a. Given (a + b) * c, the a + b will
- be evaluated first, with the sum multiplied by c. This is because *
- has higher precedence than + at the same parenthetic level, but in the
- second example the + subexpression is inside parentheses and so it is
- evaluated first. This is the hierarchy of operators.
-
- With regards to an expression like expr1 op expr2, however, the issue
- is the order of evaluation of the operands, expr1 and expr2. What
- Stroustrup says is "The order of evaluation of subexpressions is
- determined by the precedence and grouping of the operators . . . .
- Except where noted, the order of evaluation of operands of individual
- operators is undefined. In particular, if a value is modified twice in
- an expression, the result of the expression is undefined except where
- an ordering is guaranteed by the operators involved." Therefore,
- except in certain cases, we don't know whether expr1 or expr2 will be
- evaluated first. If expr2 modifies i and expr1 uses the value of i,
- there is no rule in most cases that tells us whether the value of i
- used in expr1 is the value it had before the expression had been
- reached or the value to which it had been set by expr2.
-
- Operators that guarantee an order of expression are &&, ||, and ?:.
- The left operand of logical AND is evaluated first, and if it is 0
- (false), the right operand is ignored altogether. If you have an
- expression (p && (q = r)), if p is 0, then q won't get assigned the
- value of r.
-
- The left operand of logical OR is also evaluated first. If the left
- operand evaluates to nonzero (true), then the right operand is not
- evaluated.
-
-
-
-
-